package com.yimi.yinhepda.views.sign_manager;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.yimi.yinhepda.R;
import com.pda.ym.base.BaseTitleActivity;
import com.yimi.yinhepda.utils.GpsUtil;
import com.pda.uce.commom.util.MyDialog;

import org.jetbrains.annotations.NotNull;

/**
 * 签到管理界面
 *
 * @author 000432
 * @date 2018/6/1 0001
 */

public class SelectBreakCardTypeActivity extends BaseTitleActivity {

    private static final int REQUEST_CODE_LOCATION_SETTINGS = 100;
    private static final int REQUEST_CODE_LOCATION_PER = 102;
    /**
     * 硬件标识请求权限标志
     */
    private static final int RQUEST_HARDWARE_PERMISSION = 101;
    /**
     * 硬件标识权限请求次数
     */
    private int hardwarePerCount;

    @Override
    public String getPageName() {
        return "签到管理界面";
    }

    @Override
    public int setContentViewId() {
        return R.layout.activity_select_break_card_type;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        findViewById(R.id.btn_back).setOnClickListener(this);
        findViewById(R.id.btn_jiaojian_break_card).setOnClickListener(this);
        findViewById(R.id.btn_ligang_braek_card).setOnClickListener(this);
        findViewById(R.id.btn_jiejian_breka_card).setOnClickListener(this);

        requestHardwareFlagPermission();
    }

    @Override
    protected void onResume() {
        super.onResume();

        //打卡开关，默认1-旧功能,0-新功能
        BreakCardUtil.queryBreakCardSwitch();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_LOCATION_SETTINGS) {
            checkGpsIsOpen();
        } else if (requestCode == REQUEST_CODE_LOCATION_PER) {
            requestHardwareFlagPermission();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case RQUEST_HARDWARE_PERMISSION:
                //硬件标识符请求权限
                if ((grantResults.length > 0) && (grantResults[0] != PackageManager.PERMISSION_GRANTED)) {
                    if (hardwarePerCount < 5) {
                        hardwarePerCount++;
                        ActivityCompat.requestPermissions(this,
                                new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, RQUEST_HARDWARE_PERMISSION);
                    } else {
                        //提示打开位置权限
                        openGpsPermissionDialog();
                    }
                }
                break;
            default:
                break;
        }
    }

    /**
     * 位置权限申请
     */
    private void requestHardwareFlagPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
                    ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION}, RQUEST_HARDWARE_PERMISSION);
            } else {
                checkGpsIsOpen();
            }
        }
    }

    /**
     * 打开GPS
     */
    private boolean checkGpsIsOpen() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //android6.0以上需要位置权限获取APMAC进行打卡
            if (!GpsUtil.isOPen(this)) {
                final MyDialog dialog = new MyDialog(this);
                dialog.setIcon(R.drawable.alert);
                dialog.setDialogType(MyDialog.DialogType.ALERT_DIALOG);
                dialog.setTitle("提示");
                dialog.setMessage("需要打开GPS位置功能才可以获取APMAC进行打卡！");
                dialog.setPositiveClickListener("去打开", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Intent locationIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivityForResult(locationIntent, REQUEST_CODE_LOCATION_SETTINGS);
                    }
                });
                dialog.setNegativeClickListener(R.drawable.btn_login_selector, "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        dialog.dismiss();
                    }
                });
                dialog.setCanceledOnTouchOutside(false);
                dialog.setCancelable(false);
                dialog.show();
                return false;
            }
        }
        return true;
    }

    /**
     * 手动打开位置权限提示
     */
    private void openGpsPermissionDialog() {
        MyDialog dialog = new MyDialog(this);
        dialog.setIcon(R.drawable.alert);
        dialog.setDialogType(MyDialog.DialogType.ALERT_DIALOG);
        dialog.setTitle("提示");
        dialog.setMessage("需要授权位置权限才可以获取APMAC进行打卡！");
        dialog.setPositiveClickListener( "去打开", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Uri packageURI = Uri.parse("package:" + getPackageName());
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                startActivityForResult(intent, REQUEST_CODE_LOCATION_PER);
            }
        });
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_jiaojian_break_card:
                if (checkGpsIsOpen()) {
                    toActivity(JiaoJianBreakCardActivity.class);
                }
                break;
            case R.id.btn_jiejian_breka_card:
                if (checkGpsIsOpen()) {
                    toActivity(JieJianBreakCardActivity.class);
                }
                break;
            case R.id.btn_ligang_braek_card:
                if (checkGpsIsOpen()) {
                    toActivity(LiGangBreakCardActivity.class);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public boolean showBack() {
        return false;
    }

    @Override
    public void setBack() {

    }

    @NotNull
    @Override
    public String setTitle() {
        return "请选择打卡类型";
    }

}
